import {
  planOption,
  addCheck,
  deleteCheck,
  getCheckStatus,
  getCheck,
  searchCheck,
  checkUpdate,
  checkTaskConf,
  checkTaskConfAll,
  getByCheckTaskId,
  checkFacilitys,
  facilityDetail,
  checkUser,
  geModelList,
  getProccessMsg,
  getEditorInfo,
} from 'service/actions/deviceCheck'
import { message } from 'antd'
import router from 'umi/router'
export default {
  namespace: 'deviceCheck',

  state: {
    planOptions: {},
    addCheckData: {},
    deleteData: {},
    checkStatus: {},
    detailData: {},
    list: {},
    checkUpdate: {},
    taskConf: {},
    taskConfAll: {},
    checkTaskList: [],
    checkFacility: [], // 设备选项
    facilityInfo: [], // 设备详细信息
    checkUser: [], // 点检员
    modelList: {}, // 弹框列表
    proccessMsg: [], // 流程信息,
    serachParams: {}
  },

  effects: {
    // 新增页面下拉选项
    *selectOptions({ payload }, { put }) {
      yield put({ type: 'getCheckFacility', payload: { deviceName: '' } })
      yield put({ type: 'getCheckUser', payload: { checkUserName: '' } })

    },

    // 查询： 计划状态选项
    *getPlanOption({ payload }, { put, call }) {
      const data = yield call(planOption, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { planOptions: data.data }
      })
    },

    // 添加点检计划
    *addCheck({ payload }, { put, call }) {
      const data = yield call(addCheck, payload)
      if (!data) { return }
      if (data.code === '000000') {
        yield put({
          type: 'updateState',
          payload: { initReq: true },
        });
      }
      yield put({
        type: 'updateState',
        payload: { addCheckData: data }
      })
      return data
    },

    // 删除点检计划
    *deleteCheck({ payload }, { put, call }) {
      const { searchData, id } = payload
      const data = yield call(deleteCheck, { id })
      if (!data) { return }
      if (data.code === "030200") {
        message.error(data.mesg)
        yield put({
          type: 'searchCheck',
          payload: { ...searchData }
        })
        return
      }
      message.success(data.mesg)
      yield put({
        type: 'searchCheck',
        payload: { ...searchData }
      })
    },

    // 计划启用停用
    *getCheckStatus({ payload }, { put, call }) {
      const { searchData, id, status } = payload
      const data = yield call(getCheckStatus, { id, status })
      if (!data) { return }
      if (data.code === "000000") {
        console.log('成功')
        message.success(data.mesg)
      } else {
        console.log('失败')
        message.error(data.mesg)
      }
      yield put({
        type: 'searchCheck',
        payload: { ...searchData }
      })
    },

    // 获取点检计划信息: 查看详情
    *getCheck({ payload }, { put, call }) {
      const data = yield call(getCheck, payload)
      if (!data) { return }
      if (data.code === '000000') {
        yield put({
          type: 'updateState',
          payload: { detailData: data.data }
        })
      }
      return data
    },

    // 获取点检计划信息: 编辑
    *getEditorMsg({ payload }, { put, call }) {
      const data = yield call(getEditorInfo, payload)
      if (!data) { return }
      if (data.code === '000000') {
        yield put({
          type: 'updateState',
          payload: { detailData: data.data }
        })
      } else {
        message.error(data.mesg)
        router.push('/deviceCheck')
      }
    },

    // 搜索点检计划
    *searchCheck({ payload }, { put, call }) {
      const data = yield call(searchCheck, payload)
      if (!data) { return }
      if (data.code === '000000') {
        yield put({
          type: 'updateState',
          payload: { initReq: false },
        });
      }
      yield put({
        type: 'updateState',
        payload: { list: data.data }
      })
    },

    // 修改点检计划
    *toCheckUpdate({ payload }, { put, call }) {
      const data = yield call(checkUpdate, payload)
      if (!data) { return }
      if (data.code === '000000') {
        yield put({
          type: 'updateState',
          payload: { initReq: true },
        });
      }
      yield put({
        type: 'updateState',
        payload: { checkUpdate: data }
      })
      return data
    },

    // 获取点检项目（任务）预置
    *checkTaskConf({ payload }, { put, call }) {
      const data = yield call(checkTaskConf, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { taskConf: data.data }
      })
    },

    // 查询所有的预制点检项目
    *checkTaskConfAll({ payload }, { put, call }) {
      const data = yield call(checkTaskConfAll, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { taskConfAll: data.data }
      })
    },

    // 详情也表格：点击展开获取列表信息
    *getByCheckTask({ payload }, { put, call }) {
      const data = yield call(getByCheckTaskId, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { checkTaskList: data.data }
      })
      return data
    },

    // 新增： 获取点检设备选项
    *getCheckFacility({ payload }, { put, call }) {
      const data = yield call(checkFacilitys, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { checkFacility: data.data }
      })
    },

    // 新增： 获取设备详细信息
    *getFacilityInfo({ payload }, { put, call }) {
      const data = yield call(facilityDetail, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { facilityInfo: data.data }
      })
    },

    // 新增： 获取点检员选项
    *getCheckUser({ payload }, { put, call }) {
      const data = yield call(checkUser, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { checkUser: data.data }
      })
    },

    // 新增： 获取弹框列表
    *geModelLists({ payload }, { put, call }) {
      const data = yield call(geModelList, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { modelList: data.data }
      })
    },

    // 编辑点检计划给 设备和点检员信息赋值
    *valuation({ payload }, { put }) {
      const { checkUser, facilityInfo } = payload
      yield put({ type: 'updateState', payload: { checkUser, facilityInfo } })

    },

    // 流程信息
    *getProccessMsgs({ payload }, { put, call }) {
      const data = yield call(getProccessMsg, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { proccessMsg: data.data }
      })
    },

    // 清空detail
    *cleanDetail({ _ }, { put }) {
      // yield put({
      //   type: 'updateState',
      //   payload: { detailData: {} }
      // })
    },
  },

  reducers: {
    // 更行state
    updateState(state, { payload }) {
      return { ...state, ...payload }
    },
    // 清除缓存
    clean(state) {
      return {
        ...state,
        proccessMsg: [],
      }
    },
    // 更细查询参数
    updateSerachParams(state, { payload: { serachParams } }) {
      return { ...state, serachParams }
    }
  }
}
